package org.mspring.mlog.web.module.admin.bbs;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.bbs.TopicReply;
import org.mspring.mlog.entity.bbs.TopicReply;
import org.mspring.mlog.service.bbs.TopicReplyService;
import org.mspring.mlog.web.module.admin.Admin_AbstractWidget;
import org.mspring.mlog.web.query.bbs.TopicReplyQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 回帖管理
 * 
 * @author Gao Youbo
 * @since 2013年9月5日
 */
@RequestMapping("/admin/bbs/topicReply")
@Widget
public class Admin_TopicReplyWidget extends Admin_AbstractWidget {
    @Autowired
    private TopicReplyService topicReplyService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping("/list")
    public String list(@ModelAttribute Page<TopicReply> topicReplyPage, @ModelAttribute TopicReply topicReply, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (topicReplyPage == null) {
            topicReplyPage = new Page<TopicReply>();
        }
        if (topicReplyPage.getSort() == null) {
            topicReplyPage.setSort(new Sort("id", Sort.DESC));
        }

        topicReplyPage = topicReplyService.listPage(new TopicReplyQueryCriterion(queryParams), topicReplyPage);
        model.addAttribute("topicReplyPage", topicReplyPage);
        model.addAllAttributes(queryParams);
        return "/admin/bbs/topicReply/listTopicReply";
    }

    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete")
    public String delete(@RequestParam(required = false) Long[] id, @ModelAttribute Page<TopicReply> topicReplyPage, @ModelAttribute TopicReply topicReply, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null || id.length == 0) {
            return prompt(model, "请选择要删除的回帖");
        }
        topicReplyService.deleteReply(id);
        return list(topicReplyPage, topicReply, queryParams, request, response, model);
    }
}